package cn.itcast.mq.listener;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.time.LocalDateTime;

@Component
public class SpringRabbitListener {
//    @RabbitListener(queues = "simple.queue")
//    public void listenSimpleQueue(@Payload String msg, Channel channel, Message message) {
//        System.out.println("消费者接收到simple.queue的消息：【" + msg + "】");
//        System.out.println(1/0);
//        System.out.println("处理消息");
//        try {
//            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false); // 确认
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }

    @RabbitListener(queues = "simple.queue")
    public void listenSimpleQueue(String msg) {
        System.out.println("消费者接收到simple.queue的消息：【" + msg + "】");
        System.out.println(1/0);
        System.out.println("处理消息");
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("dl.queue"),
            exchange = @Exchange("dl.direct"),
            key = {"dl"}
    ))
    public void listenDLQueue(String msg) {
        System.out.println("接收到死信队列消息：【" + msg + "】   "+ LocalDateTime.now().toString());
    }

    @RabbitListener(queues = "delay.queue")
    public void listenDelayQueue(String msg) {
        System.out.println("消费者接收到延迟队列的消息：【" + msg + "】" + LocalDateTime.now().toString());

    }
}
